package com.xiaoleng.tlgsku.service.Impl;


import com.xiaoleng.tlgsku.dao.AttributeKeyMapper;
import com.xiaoleng.tlgsku.dao.GoodsSpecsMapper;
import com.xiaoleng.tlgsku.pojo.AttributeKey;
import com.xiaoleng.tlgsku.pojo.GoodsSpecs;
import com.xiaoleng.tlgsku.service.GoodsSpecsService;
import com.xiaoleng.tlgsku.tools.Record;
import com.xiaoleng.tlgsku.tools.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsSpecsServiceImpl implements GoodsSpecsService {

    @Autowired
    private GoodsSpecsMapper goodsSpecsMapper;
    @Autowired
    private AttributeKeyMapper attributeKeyMapper;

    @Override
    public Record findimgBygoodsId(Integer valueId, Integer goodsId) {
        Record record = new Record();
        try {
            GoodsSpecs goodsSpecs = goodsSpecsMapper.findimgBygoodsId(valueId, goodsId);
            if (goodsSpecs.getId() != null) {
                record.setStatus(Status.SELECT_SUCCESS);
                record.setMsg("查询规格成功！");
                record.setData(goodsSpecs);
            } else {
                record.setStatus(Status.SELECT_FALIED);
                record.setMsg("查询规格失败！");
            }
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("查询规格异常！");
            e.printStackTrace();
        } finally {
            return record;
        }
    }

    @Override
    public Record addSpecs(GoodsSpecs goodsSpecs) {
        Record record = new Record();
        Integer count = 0;
        try {
            count = goodsSpecsMapper.addSpecs(goodsSpecs);
            if (count > 0) {
                record.setStatus(Status.SAVE_SUCCESS);
                record.setMsg("添加规格信息成功！");
            } else {
                record.setStatus(Status.SAVE_FALIED);
                record.setMsg("添加规格信息失败！");
            }
        } catch (Exception e) {
            record.setStatus(Status.SAVE_FALIED);
            record.setMsg("添加规格信异常！");
            e.printStackTrace();
        } finally {
            record.setData(count);
            return record;
        }


    }

    @Override
    public Record findlist(Integer keyId, Integer keyValueId, Integer valueId, Integer specsId) {
        Record record = new Record();
        StringBuffer goodsList = new StringBuffer();
        try {
            goodsList.append(keyId);
            goodsList.append(":");
            goodsList.append(keyValueId);
            goodsList.append(",");
            if (null != valueId && null != specsId) {
                goodsList.append(valueId);
                goodsList.append(":");
                goodsList.append(specsId);
            }
            List<GoodsSpecs> list = goodsSpecsMapper.findlist(goodsList.toString());
            if (list.size() != 0 && null != list) {
                record.setStatus(Status.SELECT_SUCCESS);
                record.setMsg(" 查询规格成功！");
                record.setData(list);
            } else {
                record.setStatus(Status.SELECT_FALIED);
                record.setMsg(" 查询规格失败！");
            }
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("查询规格异常！");
            e.printStackTrace();
        }
        return record;
    }

    /**
     * 规格详情和库存集合
     *
     * @return
     */
    @Override
    public Record info(Integer goodsId, Integer keyId, Integer keyValueId, Integer valueId, Integer specsId) {
        Record record = new Record();
        Map<String, List> map = new HashMap<String, List>();
        List<AttributeKey> list = null;
        List<GoodsSpecs> goodsSpecsList = null;
        try {
            if (null != valueId && null != specsId) {
                Record recordList = findlist(keyId, keyValueId, valueId, specsId);
                goodsSpecsList = (List<GoodsSpecs>) recordList.getData();
            } else {
                Record recordList = findlist(keyId, keyValueId, valueId, specsId);
                goodsSpecsList = (List<GoodsSpecs>) recordList.getData();
                list = attributeKeyMapper.findValuekeyBygoodsId(goodsId);
            }
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("查询异常！");
        }
        if (null != goodsSpecsList && goodsSpecsList.size() > 0) {
            map.put("goodsSpecsList", goodsSpecsList);
            record.setStatus(Status.SELECT_SUCCESS);
        }
        if (null != list && list.size() > 0) {
            map.put("attributeList", list);
            record.setStatus(Status.SELECT_SUCCESS);
        }
        if (map.size() > 0) {
            record.setMsg("查询成功");
            record.setData(map);
        } else {
            record.setMsg("查询失败！！");
        }
        return record;
    }


    /**
     * 查询规格信息
     *
     * @param goodsId 商品规格
     * @param specsList     所选规格
     * @return
     */
    @Override
    public Record findSpecsInfo(Integer goodsId, String specsList) {
        Record record = new Record();
        GoodsSpecs goodsSpecs=null;
        try {
            goodsSpecs=goodsSpecsMapper.findSpecsInfo(goodsId,specsList);
        } catch (Exception e) {
            e.printStackTrace();
            record.setMsg("规格信息查询异常!!!");
            record.setStatus(Status.SELECT_FALIED);
        }
        if(null!=goodsSpecs){
            record.setMsg("规格信息查询成功");
            record.setStatus(Status.SELECT_SUCCESS);
            record.setData(goodsSpecs);
        }else{
            record.setMsg("规格信息查询失败!!");
            record.setStatus(Status.SELECT_FALIED);
        }
        return record;
    }
}
